<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button>点击发送请求</button>

    <script src="./axios.js"></script>
    <script>
      const btn = document.querySelector('button')

      // btn.addEventListener('click', async function () {
      //   const { data: res } = await axios({
      //     url: 'http://ajax-api.itheima.net/api/province',
      //     method: 'GET',
      //   })

      //   console.log(1)

      //   console.log(res)
      // })

      //async , await 语法糖

      //   async 一定要放到函数之前
      //   经过async修饰符修饰的函数结果会变成promise对象
      // async function fn() {
      //   const res = await new Promise((resolve, rej) => {
      //     console.log(1)
      //     resolve(2)
      //   })

      //   console.log(3)

      //   console.log(res)
      // }

      // fn()

      // await 放到请求之前，一定要在有async修饰的函数中才能使用

      async function asyncFunction() {
        return new Promise((resolve) => {
          setTimeout(() => {
            resolve('Finished')
          }, 2000)
        })
      }

      async function exampleFunction() {
        console.log('Before await')
        console.log(1)
        const result = await asyncFunction()
        console.log(2)
        console.log('After await:', result)
        const result1 = await asyncFunction()
        console.log(3)
      }

      exampleFunction()
    </script>
  </body>
</html>
